﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AugustSql
{
    public static class MySqlHelper
    {
        public static bool ExsitDatabase(string db_name, string ip, string user, string pwd, int port)
        {
            bool exist = false;
            string str = string.Format("Host={0};User ID={1};Password={2};Port={3}", ip, user, pwd, port);
            MySqlConnection connection = new MySqlConnection(str);
            connection.Open();
            try
            {
                MySqlCommand createCommand = new MySqlCommand("show databases", connection);
                MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
                mySqlDataAdapter.SelectCommand = createCommand;
                DataSet dataSet = new DataSet();
                mySqlDataAdapter.Fill(dataSet);
                createCommand.Parameters.Clear();
                connection.Close();
                if (dataSet.Tables.Count > 0)
                    if (dataSet.Tables["Table"].AsEnumerable().Select(c => c.Field<string>("Database")).ToList().Contains(db_name))
                        exist = true;
            }
            catch (Exception e)
            {
                global::System.Console.WriteLine(e.Message.ToString());
            }
            connection.Close();
            return exist;
        }
        public static int CreatDataBase(string db_name, string ip, string user, string pwd, int port)
        {
            string str = string.Format("Host={0};User ID={1};Password={2};Port={3}", ip, user, pwd, port);
            MySqlConnection connection = new MySqlConnection(str);
            connection.Open();
            int result = 0;
            try
            {
                MySqlCommand mySqlCommand = new MySqlCommand($"CREATE DATABASE `{db_name}`", connection);
                result = mySqlCommand.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception e)
            {
                global::System.Console.WriteLine(e.Message.ToString());
            }
            connection.Close();
            return result;
        }
        public static bool FindColumAttribute(PropertyInfo prop, out ColumAttribute columXX)
        {
            ColumAttribute temp = new ColumAttribute();
            bool has = false;
            foreach (var at in prop.GetCustomAttributes())
            {
                if (at is ColumAttribute colum)
                {
                    has = true;
                    if (string.IsNullOrEmpty(colum.Name))
                        colum.Name = prop.Name;
                    if (colum.Type == SqlValueType.None)
                    {
                        //datetime
                        if (prop.PropertyType == typeof(DateTime))
                            colum.Type = SqlValueType.DATETIME;
                        else if (prop.PropertyType == typeof(DateTime?))
                            colum.Type = SqlValueType.DATETIME;
                        //int
                        else if (prop.PropertyType == typeof(int))
                            colum.Type = SqlValueType.INT;
                        else if (prop.PropertyType == typeof(int?))
                            colum.Type = SqlValueType.INT;
                        //string
                        else if (prop.PropertyType == typeof(string))
                        {
                            colum.Type = SqlValueType.VARCHAR;
                            if (colum.Length <= 0)
                                colum.Length = 50;
                        }
                        //float
                        else if (prop.PropertyType == typeof(float))
                            colum.Type = SqlValueType.FLOAT;
                        else if (prop.PropertyType == typeof(float?))
                            colum.Type = SqlValueType.FLOAT;
                        //long
                        else if (prop.PropertyType == typeof(long))
                            colum.Type = SqlValueType.BIGINT;
                        else if (prop.PropertyType == typeof(long?))
                            colum.Type = SqlValueType.BIGINT;

                        else if (prop.PropertyType == typeof(sbyte?))
                            colum.Type = SqlValueType.TINYINT;
                        else if (prop.PropertyType == typeof(sbyte))
                            colum.Type = SqlValueType.TINYINT;

                    }
                    if (colum.Length > 0)
                        colum.SetLength = true;
                    temp = colum;
                }
            }

            if (!has)
            {
                ColumAttribute c = new ColumAttribute();

                if (string.IsNullOrEmpty(c.Name))
                    c.Name = prop.Name;
                if (c.Type == SqlValueType.None)
                {
                    //datetime
                    if (prop.PropertyType == typeof(DateTime))
                        c.Type = SqlValueType.DATETIME;
                    else if (prop.PropertyType == typeof(DateTime?))
                        c.Type = SqlValueType.DATETIME;
                    //int
                    else if (prop.PropertyType == typeof(int))
                        c.Type = SqlValueType.INT;
                    else if (prop.PropertyType == typeof(int?))
                        c.Type = SqlValueType.INT;
                    //string
                    else if (prop.PropertyType == typeof(string))
                    {
                        c.Type = SqlValueType.VARCHAR;
                        c.Length = 50;
                    }
                    //float
                    else if (prop.PropertyType == typeof(float))
                        c.Type = SqlValueType.FLOAT;
                    else if (prop.PropertyType == typeof(float?))
                        c.Type = SqlValueType.FLOAT;
                    //long
                    else if (prop.PropertyType == typeof(long))
                        c.Type = SqlValueType.BIGINT;
                    else if (prop.PropertyType == typeof(long?))
                        c.Type = SqlValueType.BIGINT;

                    else if (prop.PropertyType == typeof(sbyte))
                        c.Type = SqlValueType.TINYINT;
                    else if (prop.PropertyType == typeof(sbyte?))
                        c.Type = SqlValueType.TINYINT;

                }
                if (c.Length > 0)
                    c.SetLength = true;
                temp = c;
            }
            columXX = temp;
            return has;
        }
    }
}
